package com.carkeeper.discover.controller;

import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.discover.service.DiscoverService;
import com.carkeeper.pojo.Blog;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
@Api(tags = "发现管理接口api",description = "发现管理接口，提供页面的增、删、改、查")
@RestController
@RequestMapping("/blog")
@CrossOrigin
public class DiscoverController {
    @Autowired
    private DiscoverService discoverService;

    /**
     * 添加
     *
     * @param map
     * @return
     */
    @PostMapping
    @ApiOperation(value = "add",notes = "添加文稿",httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "文稿标题",required = true,paramType = "body"),
            @ApiImplicitParam(name = "cover",value = "封面图片",required = true,paramType = "body"),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = true,paramType = "body"),
            @ApiImplicitParam(name = "index",value = "排序",required = true,paramType = "body"),
            @ApiImplicitParam(name = "link",value = "链接",required = true,paramType = "body"),
            @ApiImplicitParam(name = "content",value = "内容主体",required = true,paramType = "body")
    })
    public Result add(@RequestBody Map map) {

        discoverService.add(map);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * 修改文稿
     */
    @PutMapping
    @ApiOperation(value = "update",notes = "修改文稿 发布文稿 取消发布",httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title",value = "文稿标题",required = false,paramType = "body"),
            @ApiImplicitParam(name = "cover",value = "封面图片",required = false,paramType = "body"),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = false,paramType = "body"),
            @ApiImplicitParam(name = "index",value = "排序",required = false,paramType = "body"),
            @ApiImplicitParam(name = "link",value = "链接",required = false,paramType = "body"),
            @ApiImplicitParam(name = "content",value = "内容主体",required = false,paramType = "body"),
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "body")
    })
    public Result update(@RequestBody Map map) {
        discoverService.update(map);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 条件分页
     * 有条件查询根据条件查询
     * 没有就查询所有
     *
     * @param searchMap
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    @ApiOperation(value = "findPage",notes = "查询文稿列表",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "页码",required = false,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "每页显示的记录数",required = false,paramType = "query"),
            @ApiImplicitParam(name = "title",value = "文稿标题",required = false,paramType = "query"),
            @ApiImplicitParam(name = "categoryName",value = "分类名称",required = false,paramType = "query"),
            @ApiImplicitParam(name = "creator",value = "发布人",required = false,paramType = "query"),
            @ApiImplicitParam(name = "status",value = "状态",required = false,paramType = "query"),
            @ApiImplicitParam(name = "startTime",value = "查询开始时间",required = false,paramType = "query"),
            @ApiImplicitParam(name = "endTime",value = "查询结束时间",required = false,paramType = "query")
    })
    public Result findPage(@RequestParam Map searchMap, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        Map<String, Object> map = discoverService.findPage(pageNum, pageSize, searchMap);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }


    /**
     * 查询文稿详情
     *
     * @return
     */
    @ApiOperation(value = "findById",notes = "根据id查询文稿",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = false,paramType = "path"),
    })
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        Map map = discoverService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", map);
    }
}
